﻿using Reticulum.User;
using Reticulum.工具类;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Reticulum.Communication
{
    public partial class AddMyFriends : Form
    {
        public AddMyFriends()
        {
            InitializeComponent();
        }

        public static string OtherUseID = "";
        private void button1_Click(object sender, EventArgs e)
        {
            SqlDataReader reader = DBHelps.Select(string.Format("select UserName,ImagePath,UserID from Users where UserAccount='{0}'", textBox1.Text));
            if (reader.Read())
            {
                label1.Visible = true;
                pictureBox1.Visible = true;
                button3.Visible = true;
                label1.Text = reader["UserName"].ToString();
                OtherUseID = reader["UserID"].ToString();
                this.pictureBox1.BackgroundImage = ImageShow.OthersUserImage(reader["ImagePath"].ToString());
            }
            else
            {
                pictureBox1.Visible = false;
                label1.Text = "未找到该用户";
                button3.Visible = false;
            }
            reader.Close();
            DBHelps.reader = null;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void AddMyFriends_Load(object sender, EventArgs e)
        {
            //用户姓名在容器中居中
            base.OnResize(e);
            int x = (int)(0.5 * (panel2.Width - label1.Width));
            int y = label1.Location.Y;
            label1.Location = new System.Drawing.Point(x, y);

            pictureBox1.Visible = false;
            label1.Visible = false;
            button3.Visible = false;

        }

        private void button3_Click(object sender, EventArgs e)
        {
            bool result = false;
            SqlDataReader reader = DBHelps.Select(string.Format("select MyfriendID from MyFriends where UserID ={0} and MyfriendID={1}", ThisMyInfo.UserID, OtherUseID));
            if (reader.Read())
            {
                MessageBox.Show("你已存在该好友");
            }
            else if (ThisMyInfo.UserID == OtherUseID)
            {
                MessageBox.Show("不能添加自己哦（━┳━　━┳━）");
            }
            else{
                result = true;  
            }
            reader.Close();
            DBHelps.reader = null;

            if (result)
            {
                DBHelps.Command(string.Format("insert MyFriends values({0},{1})", ThisMyInfo.UserID, OtherUseID));
                DBHelps.Command(string.Format("insert MyFriends values({0},{1})", OtherUseID, ThisMyInfo.UserID));
                MessageBox.Show("好友添加成功！");

                Client.userFrom.FriendsList.Controls.Clear();
                Client.userFrom.UserFrom_Load(sender, e);
                this.Close();
            }
            
        }
    }
}
